﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Globalization;
using System.Windows.Data;

#endregion

namespace Majesty_of_Omega.GUI.ValueConverters
{
    /// <summary>
    /// This Converter enables simple string formatting while data binding in a xaml document
    /// </summary>
    /// <example>
    /// <![CDATA[
    /// 
    /// <TextBlock Text="{Binding Path=TimeOfSale, 
    ///                   Converter={StaticResource FormattingStringConverter}, ConverterParameter=' \{0:dd-MMM-yyyy hh:mm\}'}" /> 
    /// 
    /// ]]>
    /// </example>
    [ValueConversion(typeof (object), typeof (string))]
    public class FormattingStringConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// This function converts a object to a formatted string with the string.Format() function
        /// </summary>
        /// 
        ///<param name="culture">The culture to use in the converter.</param>
        ///<param name="targetType">The type of the binding target property.</param>
        ///<param name="parameter">The converter parameter to use.</param>
        ///<param name="value">The value produced by the binding source.</param>
        /// 
        /// <returns>
        /// a formatted string
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string formatString = parameter as string;
            if (formatString != null)
            {
                return string.Format(culture, formatString, value);
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// This function is not implemented
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}